<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
/**
 * @package
 * @file                 $RCSfile: init.php,v $
 * @version              $Revision: 1.0 $
 * @modifiedby           $Author: handaoliang $
 * @lastmodified         $Date: 2010/03/15 15:08:19 $
 * @copyright            Copyright (c) 2010, TinyTui2
**/
require_once("init.php");
$actions = exGet("act");
$smarty->setTpl(dirname(__FILE__));
if("" == $actions){
    $smarty->display("index.tpl");
}else{
    $db_host = trim(exPost("db_host"));
    $db_name = trim(exPost("db_name"));
    $db_user_name = trim(exPost("db_uname"));
    $db_user_pwd = trim(exPost("db_upwd"));

    if(""==$db_host || ""==$db_name || ""==$db_user_name){
        $smarty->assign("error",1);
        $smarty->display("index.tpl");
    }else{
        $sql_file = SYSTEM_DIR."/Doc/tinytui2.sql";
        try{
            $connect = mysql_connect($db_host,$db_user_name,$db_user_pwd);
            mysql_select_db($db_name);
            mysql_query("SET NAMES 'utf8'");
            $query = mysql_query('show tables from '.$db_name,$connect);
            if($query){
                while($row = mysql_fetch_array($query)&&substr($row[0],0,2) != 'v_') {
                    mysql_query("TRUNCATE TABLE `".$row[0]."`;") ;
                }
            }
            if ($str = remove_remarks(file_get_contents($sql_file))) {
                $split_str = split_sql_file($str,";");
                $split_str_count=count($split_str);
                for($i = 0;$i <$split_str_count ;$i++) {
                        $cmd_txt = $split_str[$i];
                        @mysql_query($cmd_txt,$connect) ;
                 }
           }  
        }catch (Exception $e){
            throw  new Exception($e->getMessage());
        }
        $b8_config_files = SYSTEM_DIR."/libs/b8-0.4.4/etc/config_storage.php";
        $file_handle = fopen($b8_config_files,"r");
        $file_con = "";
        while (!feof($file_handle)) {
           $file_con .= fgets($file_handle);
        }
        fclose($file_handle);

        $file_con = ereg_replace("YOUR_MYSQL_DB_HOST",$db_host,$file_con);
        $file_con = ereg_replace("YOUR_MYSQL_DB_USER_NAME",$db_user_name,$file_con);
        $file_con = ereg_replace("YOUR_MYSQL_DB_USER_PASSWORD",$db_user_pwd,$file_con);
        $file_con = ereg_replace("YOUR_MYSQL_DB_NAME",$db_name,$file_con);

        $file_handle = fopen($b8_config_files,"w+");
        fwrite($file_handle,$file_con);
        fclose($file_handle);

        $smarty->assign("secess",1);
        $smarty->display("index.tpl");
    }
}

function remove_remarks($sql) {
    $i = 0;
    while ($i < strlen($sql)) {
        if (($sql[$i] == "-"||$sql[$i] == "#") and ($i==0 or $sql[$i-1] == "\n")) {
            $j=1;
            while ($sql[$i+$j] != "\n") {
                $j++;
                if ($j+$i > strlen($sql)) break;
            }
            $sql = substr($sql,0,$i) . substr($sql,$i+$j);
        }
        $i++;
    }
    return($sql);
}

function split_sql_file($sql, $delimiter) {
    $sql = trim($sql);
    $char = "";
    $last_char = "";
    $ret = array();
    $in_string = true;
    for($i=0; $i<strlen($sql); $i++) {
        $char = $sql[$i];
        if($char == $delimiter && !$in_string) {
            $ret[] = substr($sql, 0, $i);
            $sql = substr($sql, $i + 1);
            $i = 0;
            $last_char = "";
        }
        if($last_char == $in_string && $char == ")")  $in_string = false;
        if($char == $in_string && $last_char != "\\") $in_string = false;
        elseif(!$in_string && ($char == "\"" || $char == "'") && ($last_char != "\\")) $in_string = $char;
        $last_char = $char;
    }
    if (!empty($sql)) $ret[] = $sql;
    return($ret);
} 
?>